<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>定时任务</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="/lib/layui-v2.5.4/css/layui.css" media="all">
    <link rel="stylesheet" href="/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <a class="layui-btn layui-btn-normal" id="jobAdd">新增任务</a>
        <table class="layui-hide" id="job" lay-filter="job"></table>
    </div>
    <script type="text/html" id="jobBar">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script type="text/html" id="checkboxTpl">
        <input type="checkbox" value="{{d.id}}" name="isStatus" lay-skin="switch" lay-filter="isStatus"
               lay-text="已启用|未启用"
               title="锁定" {{ d.status== 1 ? 'checked' : '' }}>
    </script>

</div>
</body>
<script src="/lib/layui-v2.5.4/layui.js" charset="utf-8"></script>
<script th:inline="javascript">
    /*<![CDATA[*/
    layui.use(['table', 'form'], function () {
        var table = layui.table,
            form = layui.form,
            $ = layui.jquery;
        // 表格
        table.render({
            elem: '#job',
            url: '/api/admin/setting/task/list',
            page: true,
            cols: [
                [
                    {field: 'id', title: 'ID', align: 'center', hide: true},
                    {field: 'jobName', title: '名称', align: 'center'},
                    {field: 'cron', title: '任务表达式', align: 'center'},
                    {field: 'status', title: '是否启用', templet: '#checkboxTpl', unresize: true, align: 'center'},
                    {field: 'note', title: '描述', align: 'center'},
                    {fixed: 'right', title: '操作', toolbar: '#jobBar', align: 'center'}
                ]
            ]
        });
        //监听工具条
        table.on('tool(job)', function (obj) {
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'del') { //删除
                layer.confirm('真的删除行么', function (index) {
                    //向服务端发送删除指令
                    var param = {
                        "id": data.id
                    }
                    $.ajax({
                        url: "/api/admin/setting/task/confirm/delete/" + data.id,
                        type: "DELETE",
                        data: param,
                        success: function (rst) {
                            layer.msg(rst.msg);
                            if (rst.code === '1') {
                                setTimeout(function () {
                                    table.reload('job', data.field);
                                }, 1000);
                            }
                        }
                    });
                    layer.close(index);
                });
            } else if (layEvent === 'edit') { //编辑
                layer.open({
                    id: 'job-edit',
                    type: 2,
                    area: ['700px', '450px'],
                    fix: false,
                    maxmin: true,
                    shadeClose: false,
                    shade: 0.4,
                    title: "编辑定时任务",
                    content: '/admin/setting/task/edit?id=' + data.id
                });
            }
        });
        //监听是否锁定
        form.on('switch(isStatus)', function (data) {
            var param = {
                "id": data.value,
                "status": this.checked
            }
            $.ajax({
                url: "/api/admin/setting/task/confirm/edit/status",
                type: "PUT",
                data: param,
                success: function (rst) {
                    layer.msg(rst.msg);
                }
            });
        });
        $('#jobAdd').click(function () {
            layer.open({
                id: 'job-add',
                type: 2,
                area: ['700px', '450px'],
                fix: false,
                maxmin: true,
                shadeClose: false,
                shade: 0.4,
                title: "新增定时任务",
                content: '/admin/setting/task/add'
            });
        });
    });
    /*]]>*/
</script>
</html>